<?php
namespace addon\cms\controller;
use app\common\controller\AddonFrontBase;
use tpfcore\Core;
use think\Cookie;
use think\Template;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * ============================================================================
 */
class Vpage extends AddonFrontBase
{
    
    public function index(){
        if(empty($this->param['pageid'])){
            $this->tip([40044,"非法请求",null]);
        }
        $list = Core::loadAddonModel("Spage")->getSpage(["where"=>["id"=>$this->param['pageid']]]);
    	if(empty($list)){
            $this->tip([40044,"你访问的单页不存在",null]);
        }

        $file_name=md5($list[0]['id']);

        $sheader=$list[0]['sheader'];
        $sbody=htmlspecialchars_decode($list[0]['sbody']);
        $sfooter=$list[0]['sfooter'];

        $content = file_get_contents("addon/cms/view/".$list[0]['template']);

        $content = str_replace('{$template.sheader}', $sheader, $content);
        $content = str_replace('{$template.sbody}', $sbody, $content);
        $content = str_replace('{$template.sfooter}', $sfooter, $content);

        $new_file="addon/cms/view/cache/$file_name.html";

        $dir_path = str_replace(basename($new_file), "", $new_file);

        if(!is_dir($dir_path)){
            $flag = @mkdir($dir_path,0777,true);
        }

        file_put_contents($new_file, $content);

        $this->addonTemplate("cache/$file_name",[
            "list"=>$list
        ]);
    }
}
